package com.xn.app.muying.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JSONUtil {
	
	/**
	 * @Description: json object 转 map 递归
	 * @param @param json
	 * @param @param map
	 * @return void
	 * @throws
	 */
	public static void json2map(JSONObject json, Map<String, Object> map) {
		for(Iterator it = json.keys(); it.hasNext();) {
			String key = (String) it.next();
			Object value = null;
			try {
				value = json.get(key);
				if(value instanceof JSONObject) {
					Map<String, Object> nj = new HashMap<String, Object>();
					json2map((JSONObject) value, nj);
					value = nj;
				} else {
					value = StringUtil.escapeNull(value.toString());
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			map.put(key, value);
		}
		
	}
	
	/**
	 * @Description: json object 转 map
	 * @param json
	 * @return
	 */
	public static HashMap<String, String> toMap(JSONObject json) {
		HashMap<String, String> map = new HashMap<String, String>();
		Iterator<?> it = json.keys();
		while(it.hasNext()) {
			String key = it.next().toString();
			map.put(key, StringUtil.escapeNull(String.valueOf(json.opt(key))));
		}
		return map;
	}
	
	/**
	 * @Description: JSONArray 转 List<Map<String,String>>
	 * @param @param jsonAry
	 * @param @return
	 * @return List<Map<String,String>>
	 * @throws
	 */
	public static ArrayList<HashMap<String, String>> jsonAryToListMap(JSONArray jsonAry){
		if(jsonAry == null || jsonAry.length() == 0){
			return null;
		}
		ArrayList<HashMap<String, String>> listMap = new ArrayList<HashMap<String, String>>();
		JSONObject json = null;
		HashMap map = null;
		int length = jsonAry.length();
		for(int i=0; i<length; i++){
			try {
				json = jsonAry.getJSONObject(i);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			if(json == null) continue;
			map = toMap(json);
			if(map == null) continue;
			listMap.add(map);
		}
		return listMap;
	}
	
	/**
	 * @Description: JSONArray 转 List<Map<String,String>>
	 * @param @param jsonAry
	 * @param @return
	 * @return List<Map<String,String>>
	 * @throws
	 */
	public static List<Map<String, String>> jsonAryToListMap2(JSONArray jsonAry){
		if(jsonAry == null || jsonAry.length() == 0){
			return null;
		}
		List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
		JSONObject json = null;
		HashMap map = null;
		int length = jsonAry.length();
		for(int i=0; i<length; i++){
			try {
				json = jsonAry.getJSONObject(i);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			if(json == null) continue;
			map = toMap(json);
			if(map == null) continue;
			listMap.add(map);
		}
		return listMap;
	}
	
	/**
	 * @Description: json字符串转json对象
	 * @param @param str
	 * @param @return
	 * @return JSONObject
	 * @throws
	 */
	public static JSONObject strToJson(String str){
		if(!StringUtil.valid(str, true)) {
			return null;
		}
		JSONObject json = null;
		try {
			json = new JSONObject(str);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return json;
	}
	
	/**
	 * @Description: json数组字符串转JSONArray对象
	 * @param @param str
	 * @param @return
	 * @return JSONArray
	 * @throws
	 */
	public static JSONArray strToJsonAry(String str){
		if(!StringUtil.valid(str, true)) {
			return null;
		}
		JSONArray jsonAry = null;
		try {
			jsonAry = new JSONArray(str);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return jsonAry;
	}
	
}
